package com.noble.glamour.module.local.msgview.chatview.base;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.noble.glamour.R;
import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.module.local.album.ImgSelectUtil;
import com.noble.glamour.module.local.msgview.ChatAdapter;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.ui.agora.model.AgoraConstant;
import com.noble.glamour.ui.user.check.bean.VideoConfig;
import com.noble.library.log.PToast;

/**
 * 发信功能扩展Base
 */
public class ChatViewPanel extends ChatBasePanel implements RequestComplete {
    private VideoConfig config;

    public ChatViewPanel(Context context, ChatAdapter.ChatInstance chatInstance) {
        super(context, chatInstance);
    }

    /**
     * 控制当前控件组的显示状态。如果显示中则隐藏，否则显示。
     */
    public void showToggle() {
        if (contentView == null) {
            return;
        }

        show(!(contentView.isShown()));
    }

    /**
     * 控制当前控件组的显示状态。
     *
     * @param show 显示状态。
     */
    public void show(boolean show) {
        if (contentView == null) {
            return;
        }

        if (show) {
            contentView.setVisibility(View.VISIBLE);
        } else {
            contentView.setVisibility(View.GONE);
        }
    }

    /**
     * 控制当前控件组的显示状态。
     *
     * @param visibility 显示状态。
     */
    public void show(int visibility) {
        if (contentView == null) {
            return;
        }

        contentView.setVisibility(visibility);
    }

    /**
     * 发送图片消息
     */
    public void sendIMGMsg() {
        final ChatAdapter chatAdapter = getChatInstance().chatAdapter;
        ImgSelectUtil.getInstance().pickPhotoGallery(getContext(), new ImgSelectUtil.OnChooseCompleteListener() {
            @Override
            public void onComplete(String... path) {
                if (path.length > 0) {
                    ModuleMgr.getChatMgr().sendImgMsg(chatAdapter.getChannelId(), chatAdapter.getWhisperId(), path[0],
                            chatAdapter.getChatInfo().getKnow());
                }
            }
        });
    }

    /**
     * 发送视频消息
     */
    public void sendVideoMsg() {
        // 获取他人音视频配置
//        ModuleMgr.getCenterMgr().reqVideoChatConfig(getChatInstance().chatAdapter.getLWhisperId(), this);
        ModuleMgr.getRtcChatMgr().inviteVideo((Activity) getContext(), getChatInstance().chatAdapter.getLWhisperId(), AgoraConstant.APPEAR_TYPE_NO, "");

    }

    private void sendVideo() {
        ChatAdapter chatAdapter = getChatInstance().chatAdapter;
        if (ModuleMgr.getCenterMgr().getMyInfo().isMan() && (config == null || !config.isVideoChat())) {
            PToast.showShort(getContext().getString(R.string.user_other_not_video_chat));
            return;
        }
        boolean isShow;
        UserLight info = chatAdapter.getUserInfo(chatAdapter.getLWhisperId());
        if (ModuleMgr.getCenterMgr().getMyInfo().isMan()) {
            isShow = true;
        } else {
            isShow = false;
        }
        ModuleMgr.getRtcChatMgr().inviteVideo((Activity) getContext(), chatAdapter.getLWhisperId(), AgoraConstant.APPEAR_TYPE_NO, "");
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqVideoChatConfig) {
            if (response.isOk()) {
                config = (VideoConfig) response.getBaseData();
            }
            sendVideo();
        }
    }
}
